//
//  CollectionCellForHot.m
//  Movie
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "CollectionCellForHot.h"
#import "MovieModel.h"
#import "CollectionCellForFirst.h"
#import "NetworkHandeler.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>

@interface CollectionCellForHot ()<UICollectionViewDelegate,UICollectionViewDataSource,NetworkHandelerDelegate>

@property (nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain)UICollectionView *collectionV;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)NSString *str;
@property (nonatomic, assign)NSInteger pag;
@property (nonatomic, copy) NSString *urlForindex;

@end


@implementation CollectionCellForHot
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {

//
//        self.str = [NSString stringWithFormat: @"http://service.vmovier.com/api/post/getPostInCate?cateid=%@&p=%ld",self.urlForindex,++self.pag];
//        self.pag = 1;
//        [self passData:self.str];
        
        [self createCollectionView];
    }
    
    return self;
}


- (void)createCollectionView{
    
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(self.frame.size.width - 30, self.frame.size.height / 2 + 10);
    
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 20, 15);
    self.flowLayout.footerReferenceSize = CGSizeMake(0, 130);
    
    self.collectionV = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    self.collectionV.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionV];
    
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    self.collectionV.backgroundColor = [UIColor colorWithRed:40 / 255.f green:40 / 255.f blue:40 / 255.f alpha:1];
    
    [self.collectionV registerClass:[CollectionCellForFirst class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.contentView addSubview:self.collectionV];
    
    self.collectionV.showsHorizontalScrollIndicator = NO;
     self.collectionV.mj_header.automaticallyChangeAlpha = YES;


}

- (void)passData:(NSString *)data {

    
    self.urlForindex = data;
    
    NSString *string = [NSString stringWithFormat:@"http://service.vmovier.com/api/post/getPostInCate?cateid=%@&p=1", self.urlForindex];
    
    
    [NetworkHandeler handelerJSONWithURL:string delegate:self];
    [self.collection reloadData];

}


- (void)handelerDidComplete:(id)result{

    self.arr = [NSMutableArray array];

    NSArray *array = [result objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
        
        MovieModel *model = [[MovieModel alloc]init];
        model.coverForDetail = [dic objectForKey:@"image"];
        model.title = [dic objectForKey:@"title"];
        model.rating = [dic objectForKey:@"rating"];
        model.share_num = [dic objectForKey:@"share_num"];
        model.postid = [dic objectForKey:@"postid"];
        
        [self.arr addObject:model];
    }
    
    [self.collectionV reloadData];

//    [self.collectionV.mj_footer endRefreshing];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionCellForFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    MovieModel *model = [self.arr objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    return cell;
}


/** 通过 delegate 回调出去.*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(didClickSelect:)]) {
        
        [self.delegate didClickSelect:[self.arr objectAtIndex:indexPath.item]];
    }
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

}


- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return layoutAttributes;
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
}

@end
